package com.itboye.ihomebank.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;

import com.itboye.ihomebank.R;
import com.itboye.ihomebank.base.MyApplcation;
import com.itboye.ihomebank.bean.ResultEntity;
import com.itboye.ihomebank.bean.WoDeFangYuanBean;
import com.itboye.ihomebank.constants.NetPublicConstant;
import com.itboye.ihomebank.constants.SPContants;
import com.itboye.ihomebank.presenter.HousePresenter;
import com.itboye.ihomebank.support.commonadapter.MutiplyCommonAdapter;
import com.itboye.ihomebank.support.commonadapter.ViewHolder;
import com.itboye.ihomebank.util.ByAlert;
import com.itboye.ihomebank.util.SPUtils;
import com.itboye.ihomebank.util.XImageLoader;

import java.util.Iterator;
import java.util.List;
import java.util.Observable;
import java.util.Observer;
/**
 * 我的房源
 *
 */
public class WodeFangyYuanAdapter02 extends
		MutiplyCommonAdapter<WoDeFangYuanBean> implements Observer{
	HousePresenter housePresenter;
	private String uid;
	int position;
	private List<WoDeFangYuanBean> beans;
	protected String tempHouseNo;
	public WodeFangyYuanAdapter02(Context context, List<WoDeFangYuanBean> datas,
			int... layoutId) {
		super(context, datas, layoutId);
		beans = datas;
		housePresenter = new HousePresenter(this);
		uid = SPUtils.get(context, null, SPContants.USER_ID, "") + "";
	}

	@Override
	public int getItemViewType(int position) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public void getItemPosition(int position) {
		this.position = position;

	}

	@Override
	public void convert(ViewHolder holder, final WoDeFangYuanBean t, int type,
			final int position) {
		holder.setText(R.id.fangyuan_title, t.getTitle());
		holder.setText(R.id.fangyuan_addr, t.getHouseRoom()+"室/"+t.getHouseHall()+"厅/"+t.getHouseArea()+"㎡"+"/"+t.getHouseDir());
		holder.setText(R.id.fangyuan_money, "¥"+Double.parseDouble(t.getRent())/100+"元/月");
		XImageLoader.load(NetPublicConstant.IMAGE_URL + t.getImgId()+SPContants.SHUIYIN,
				(ImageView) holder.getView(R.id.fangyuan_img));
		
		String biao01=t.getTags();
		String[] str = {};
		if(biao01!=null){
			str = biao01.split(",");
		}
		holder.removeAllViews(R.id.fabu_flowLayout02);
		for (int i = 0; i < str.length; i++) {
			View view = LayoutInflater.from(MyApplcation.ctx).inflate(
					R.layout.item_biaoqian, null);
			final TextView btn_name = (TextView) view
					.findViewById(R.id.btn_name);
			btn_name.setText(str[i]);
			btn_name.setTextSize(13);
			btn_name.setPadding(10,5,10,5);
			
			holder.addView(R.id.fabu_flowLayout02, view);
		}
		holder.setOnClickListener(R.id.fangyuan_quxiao, position, new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
				tempHouseNo = t.getHouseNo();
				
				housePresenter.shenQingQianYue(uid, t.getHouseNo(), "", "2","","","","");
			}
		});
		if (t.getApplyStatus().equals("0")){
			holder.setText(R.id.fangyuan_tag, "房东审核中");
		}else{
			holder.setText(R.id.fangyuan_tag, "审核通过");
		}

	}
	@Override
	public void update(Observable observable, Object data) {
		ResultEntity entity = handlerError(data);
		if (entity != null) {
			if (entity.getEventType() == HousePresenter.qianYueShenQing_success) {
				ByAlert.alert(entity.getData());
				
				Iterator<WoDeFangYuanBean> iterator = beans.iterator();
				while (iterator.hasNext()) {
					WoDeFangYuanBean bean = iterator.next();
					if (tempHouseNo.equals(bean.getHouseNo())) {
						iterator.remove();
						notifyDataSetChanged();
						break;
					}
				}
				
			} else if (entity.getEventType() == HousePresenter.qianYueShenQing_fail) {
				ByAlert.alert(entity.getData());
			}
		}
	}
	
}
